package com.yami.shop.common.util;

import org.apache.commons.codec.binary.Base64;

import java.io.*;


public class Base64Utils {
    
    private static final String charset = "utf-8";

    /**
     * base64解密
     * @param data   需要解密的内容
     * @return  返回解密后的内容，如果出现异常则返回null
     */
    public static String decode(String data) {
        try {
            if (null == data) {
                return null;
            }
            return new String(Base64.decodeBase64(data.getBytes(charset)), charset);
        } catch (UnsupportedEncodingException e) {
            System.out.println(String.format("字符串：%s，解密异常", data));
        }
        return null;
    }

    /**
     * 将 BASE64 编码的字符串 s 进行解码
     *
     * @param s
     * @return
     */
    public static byte[] decodeToHnaPay(String s) {
        if (s == null) return null;
        try {
            byte[] b = java.util.Base64.getDecoder().decode(s);
            return b;
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * BASE64字符串解码为二进制数据
     * @param base64
     * @return
     * @throws UnsupportedEncodingException 
     * @throws Exception
     */
    public static byte[] decodeToByte(String base64) throws UnsupportedEncodingException{
    	return Base64.decodeBase64(base64.getBytes("utf-8"));
    }

    /**
     * base64加密
     * @param data   需要加密的内容
     * @return     返回加密后的内容，如果传入字符串为空或出现异常，则返回null
     */
    public static String encode(String data) {
        try {
            if (StringUtils.isEmpty(data)) {
                return null;
            }
            return new String(Base64.encodeBase64(data.getBytes(charset)), charset);
        } catch (UnsupportedEncodingException e) {
            System.out.println(String.format("字符串：%s，加密异常", data));
        }

        return null;
    }
    
    /**
     * 二进制数据编码为BASE64字符串
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encodeBase64String(bytes));
    }


    /**
     * 图片转化成base64字符串
     * @param imgPath  需要处理的图片文件
     * @return   返回图片转换后的字符串
     */
    public static String getImageBase64Str(String imgPath){
        byte[] data = null;
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64 base64 = new Base64();
        return base64.encodeToString(data);
    } 
    
    /**
     * 
     * @param imgStr
     * @return   
     */
    /**
     * base64字符串转化成图片
     * @param imgStr  base64字串
     * @param targetPath   生成图片的目标路径
     * @return  返回转换后的base64编码
     */
    public static boolean generateImage(String imgStr,String targetPath){   //对字节数组字符串进行Base64解码并生成图片    
        if (imgStr == null) //图像数据为空    
            return false;
		Base64 base64 = new Base64();
        try {    
            //Base64解码    
        	byte[] b = base64.decode(imgStr.getBytes());
            for(int i=0;i<b.length;++i){    
                if(b[i]<0)    
                {//调整异常数据    
                    b[i]+=256;    
                }    
            }
            //生成jpeg图片    
            OutputStream out = new FileOutputStream(targetPath);        
            out.write(b);    
            out.flush();    
            out.close();    
            return true;    
        }catch (Exception e){    
            return false;    
        }    
    }

    public static InputStream base64ToInputStream(String base64string){
        ByteArrayInputStream stream = null;
        try {
            byte[] bytes = Base64.decodeBase64(base64string);
            stream = new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stream;
    }

}
